public class test8 {
    public String convert(String s, int numRows) {
        //处理边界情况
        if (numRows == 1) {
            return s;
        }
        int len = s.length();
        int d = 2 * numRows - 2;
        StringBuilder ret = new StringBuilder();
        //处理第一行
        for (int i = 0; i < len; i += d) {
            char a = s.charAt(i);
            ret.append(a);
        }
        //处理中间行
        for (int i = 1; i < numRows - 1; i++) {
            StringBuilder temp = new StringBuilder();
            for (int j = i, k = d - i; j < len; j += d, k += d) {
                char a = s.charAt(j);
                temp.append(a);
                if(k<len){
                    char b = s.charAt(k);
                    temp.append(b);
                }
            }
            ret.append(temp);
        }
        //处理最后一行
        for (int i = numRows - 1; i < len; i += d) {
            char a = s.charAt(i);
            ret.append(a);
        }
        return ret.toString();
    }
}
